﻿using FastCloud.Base.Config;
using FastIOC.Attribute;
using FastORM.Applicaiton;
using FastORM.Entity;
using FastORM.Enum;
using FastORM.Mapping;
using FastUtil.Config;
using FastUtil.Convert;
using FastUtil.Extra;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using static FastIOC.Enum.CotainerEnum;

namespace FastCloud.Base.Desgin
{
    [Component]
    public class CommonDTO
    {
        private CommonDao @CommonDao;
        public CommonDTO()
        {
            @CommonDao = new CommonDao(DataBaseClass.MySql, AppConfigUtil.Configuration["ConnectionStrings:Cloud_ConnectionString"]);
        }
        public DataTable SelectPage(string TableName, string StrColumn, string StrWhere, string StrOrder,List<ParamEntity> ParamList = null)
        {
            int PageIndex = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageIndex"));
            int PageSize = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageSize"));
            DataTable result = SelectPage(TableName, StrColumn, StrWhere, StrOrder, PageIndex, PageSize, ParamList);
            return result;
        }
        public DataTable SelectPage(string TableName, string StrColumn, string StrWhere, string StrOrder, int PageIndex, int PageSize, List<ParamEntity> ParamList = null)
        {
            string sql = string.Format("select {0} from {1} where ", StrColumn, TableName);
            if (!string.IsNullOrEmpty(StrWhere))
            {
                sql += StrWhere;
            }
            if (!string.IsNullOrEmpty(StrOrder))
            {
                sql += " order by " + StrOrder;
            }
            if (PageIndex == 0)
            {
                PageIndex = 1;
            }
            if (PageSize != 0)
            {
                sql += string.Format(" limit {0},{1}", (PageIndex - 1) * PageSize, PageSize);
            }
            DataTable result = @CommonDao.FindViewList(sql, ParamList);
            return result;
        }
        public DataTable Select(string TableName, string StrColumn, string StrWhere, string StrOrder, List<ParamEntity> ParamList = null)
        {
            string sql = string.Format("select {0} from {1} where  ", StrColumn, TableName);
            if (!string.IsNullOrEmpty(StrWhere))
            {
                sql += StrWhere;
            }
            if (!string.IsNullOrEmpty(StrOrder))
            {
                sql += " order by " + StrOrder;
            }
            DataTable result = @CommonDao.FindViewList(sql, ParamList);
            return result;
        }
        public DataTable SelectGroup(string TableName, string StrColumn, string StrWhere, string StrGroup, List<ParamEntity> ParamList = null)
        {
            string sql = string.Format("select {0} from {1} where  ", StrColumn, TableName);
            if (!string.IsNullOrEmpty(StrWhere))
            {
                sql += StrWhere;
            }
            if (!string.IsNullOrEmpty(StrGroup))
            {
                sql += " group by " + StrGroup;
            }
            DataTable result = @CommonDao.FindViewList(sql, ParamList);
            return result;
        }
        public int SelectCount(string TableName, string StrWhere, List<ParamEntity> ParamList = null)
        {
            string sql = string.Format("select count(*) from {0} where ", TableName);
            if (!string.IsNullOrEmpty(StrWhere))
            {
                sql += StrWhere;
            }
            int result = @CommonDao.ExecuteToInt(sql, ParamList);
            return result;
        }
        public List<TEntity> SelectPage<TEntity>(string StrColumn, string StrWhere, string StrOrder, List<ParamEntity> ParamList = null) where TEntity : BaseEntity
        {
            int PageIndex = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageIndex"));
            int PageSize = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageSize"));
            List<TEntity> result = SelectPage<TEntity>(StrColumn, StrWhere, StrOrder, PageIndex, PageSize, ParamList);
            return result;
        }
        public List<TEntity> SelectPage<TEntity>(string StrColumn, string StrWhere, string StrOrder, int PageIndex, int PageSize, List<ParamEntity> ParamList = null) where TEntity : BaseEntity
        {
            List<TEntity> result = new List<TEntity>();
            MapperEntity mapper = StructMapping.GetInstance().GetMapperEntity(typeof(TEntity));
            DataTable dt = SelectPage(mapper.TableName, StrColumn, StrWhere, StrOrder, PageIndex, PageSize, ParamList);
            foreach (DataRow item in dt.Rows)
            {
                result.Add(mapper.Map<TEntity>(item));
            }
            return result;
        }
        public List<TEntity> Select<TEntity>(string TableName, string StrColumn, string StrWhere, string StrOrder, List<ParamEntity> ParamList = null) where TEntity : BaseEntity
        {
            List<TEntity> result = new List<TEntity>();
            MapperEntity mapper = StructMapping.GetInstance().GetMapperEntity(typeof(TEntity));
            DataTable dt = Select(TableName, StrColumn, StrWhere, StrOrder, ParamList);
            foreach (DataRow item in dt.Rows)
            {
                result.Add(mapper.Map<TEntity>(item));
            }
            return result;
        }
        public List<TEntity> Select_Cache<TEntity>() where TEntity : BaseEntity
        {
            List<TEntity> result = new List<TEntity>();
            MapperEntity mapper = StructMapping.GetInstance().GetMapperEntity(typeof(TEntity));
            if (!RedisUtil.ExistKey(mapper.TableName))
            {
                //返回数据库查询结果集并存入缓存
                List<TEntity> DBEntityList = Select<TEntity>(mapper.TableName, "*", "1=1", "");
                foreach (TEntity Entity in DBEntityList)
                {
                    string RowGuid = ValueConvert.ConvertToString(mapper.GetKeyValue(Entity));
                    RedisUtil.SetHash(mapper.TableName, RowGuid, JsonConvert.SerializeObject(Entity), CloudUtil.GetRedisCacheDBNum());
                    result.Add(Entity);
                }
            }
            else
            {
                Dictionary<string, object> dic = RedisUtil.GetAllHash(mapper.TableName, CloudUtil.GetRedisCacheDBNum());
                foreach (var item in dic)
                {
                    result.Add(JsonConvert.DeserializeObject<TEntity>(ValueConvert.ConvertToString(item.Value)));
                }
            }
            return result;
        }

        public object ExecuteScar(string SQL, List<ParamEntity> ParamList = null)
        {
            object result = @CommonDao.ExecuteScar(SQL, ParamList);
            return result;
        }

        public string ExecuteToString(string SQL, List<ParamEntity> ParamList = null)
        {
            string result = @CommonDao.ExecuteToString(SQL, ParamList);
            return result;
        }

        public DateTime ExecuteToDateTime(string SQL, List<ParamEntity> ParamList = null)
        {
            DateTime result = @CommonDao.ExecuteToDateTime(SQL, ParamList);
            return result;
        }

        public object ExecuteToDecimal(string SQL, List<ParamEntity> ParamList = null)
        {
            decimal result = @CommonDao.ExecuteToDecimal(SQL, ParamList);
            return result;
        }

        public double ExecuteToDouble(string SQL, List<ParamEntity> ParamList = null)
        {
            double result = @CommonDao.ExecuteToDouble(SQL, ParamList);
            return result;
        }

        public int ExecuteToInt(string SQL, List<ParamEntity> ParamList = null)
        {
            int result = @CommonDao.ExecuteToInt(SQL, ParamList);
            return result;
        }

        public void ExecuteSQL(string SQL, List<ParamEntity> ParamList = null)
        {
           @CommonDao.ExecuteSQL(SQL, ParamList);
        }
    }
}
